package com.bp.hearthealth.pulse.lnapps.ads.model

import com.bp.hearthealth.pulse.lnapps.bp_extension.LogUtil
import com.bp.hearthealth.pulse.lnapps.bp_utils.Constants
import com.bp.hearthealth.pulse.lnapps.bp_utils.DataStoreUtil
import com.bp.hearthealth.pulse.lnapps.bp_utils.SpUtils
import com.bp.hearthealth.pulse.lnapps.firbase.FireBaseUtils
import java.text.SimpleDateFormat
import java.util.Date

object AdsCount {

    const val KEY_CLICK_COUNT = "click_count"//广告点击次数
    const val KEY_DISPLAY_COUNT = "display_count"//广告展示次数
    private const val KEY_LAST_DATE = "last_date"//当天时间

    fun isToday(): Boolean {
        val dateFormat = SimpleDateFormat("yyyy-MM-dd")
        val today = dateFormat.format(Date())
        val lastDate = DataStoreUtil.getSyncData(KEY_LAST_DATE, "")
        return today == lastDate
    }

    fun initDate(vararg update: Boolean) {
        val dateFormat = SimpleDateFormat("yyyy-MM-dd")
        val today = dateFormat.format(Date())
        val lastDate = DataStoreUtil.getSyncData(KEY_LAST_DATE, "")
        //firebase更新时，重新获取firebase云端数据进行保存
        if (today != lastDate || update.isNotEmpty()) {
            saveFirebaseData()
            DataStoreUtil.putSyncData(KEY_CLICK_COUNT, 0)
            DataStoreUtil.putSyncData(KEY_DISPLAY_COUNT, 0)
            DataStoreUtil.putSyncData(KEY_LAST_DATE, today)
        }
    }

    fun incrementClickCount() {
        // 获取当前日期
        val dateFormat = SimpleDateFormat("yyyy-MM-dd")
        val today = dateFormat.format(Date())
        val lastDate = DataStoreUtil.getSyncData(KEY_LAST_DATE, "")

        if (today != lastDate) {
            DataStoreUtil.putSyncData(KEY_CLICK_COUNT, 0)
            DataStoreUtil.putSyncData(KEY_DISPLAY_COUNT, 0)
            DataStoreUtil.putSyncData(KEY_LAST_DATE, today)
        }

        val clickCount = DataStoreUtil.getSyncData(KEY_CLICK_COUNT, 0)
        DataStoreUtil.putSyncData(KEY_CLICK_COUNT, clickCount + 1)
    }

    fun incrementDisplayCount() {
        // 获取当前日期
        val dateFormat = SimpleDateFormat("yyyy-MM-dd")
        val today = dateFormat.format(Date())
        val lastDate = DataStoreUtil.getSyncData(KEY_LAST_DATE, "")

        if (today != lastDate) {
            DataStoreUtil.putSyncData(KEY_CLICK_COUNT, 0)
            DataStoreUtil.putSyncData(KEY_DISPLAY_COUNT, 0)
            DataStoreUtil.putSyncData(KEY_LAST_DATE, today)
            //当天全屏广告展示次数达到后是否埋点
            SpUtils.saveBoolean(Constants.LIMIT_30, false)
        }

        val displayCount = DataStoreUtil.getSyncData(KEY_DISPLAY_COUNT, 0)
        DataStoreUtil.putSyncData(KEY_DISPLAY_COUNT, displayCount + 1)
    }

    private fun saveFirebaseData() {
        /**
         * 总广告位控制
         */
        val adAppControl = FireBaseUtils.getBoolean(AdsSwitch.adAppControl)
//        if (adAppControl) {
//            LogUtil.i("Firebase 加载全局广告")
//            PreloadAds.loadAds(true)
//        }
        LogUtil.i("Firebase获取总开关 $adAppControl")
        //总开关
        SpUtils.saveBoolean(
            AdsSwitch.adAppControl, FireBaseUtils.getBoolean(AdsSwitch.adAppControl)
        )
        LogUtil.i("Firebase存储 总开关：${SpUtils.getBoolean(AdsSwitch.adAppControl)}")
        SpUtils.saveBoolean(
            AdsSwitch.adNativeControl, FireBaseUtils.getBoolean(AdsSwitch.adNativeControl)
        )
        SpUtils.saveBoolean(
            AdsSwitch.adBannerControl, FireBaseUtils.getBoolean(AdsSwitch.adBannerControl)
        )
        SpUtils.saveBoolean(
            AdsSwitch.adOpenControl, FireBaseUtils.getBoolean(AdsSwitch.adOpenControl)
        )

        SpUtils.saveBoolean(
            AdsSwitch.adRVControl, FireBaseUtils.getBoolean(AdsSwitch.adRVControl)
        )
        SpUtils.saveBoolean(
            AdsSwitch.adInterControl, FireBaseUtils.getBoolean(AdsSwitch.adInterControl)
        )
        /**
         * 广告展示点击控制
         */
        SpUtils.saveString(
            AdsSwitch.userDailyMaxAdsClickNum,
            FireBaseUtils.getString(AdsSwitch.userDailyMaxAdsClickNum)
        )
        SpUtils.saveString(
            AdsSwitch.userDailyMaxVideoAdsShowNum,
            FireBaseUtils.getString(AdsSwitch.userDailyMaxVideoAdsShowNum)
        )

        /**
         * 广告位控制
         */
        //原生广告位
        SpUtils.saveBoolean(
            AdsSwitch.adNativeLanguage, FireBaseUtils.getBoolean(AdsSwitch.adNativeLanguage)
        )
        SpUtils.saveBoolean(
            AdsSwitch.adNativeGender, FireBaseUtils.getBoolean(AdsSwitch.adNativeGender)
        )
        //第一次启动性别选择页后_插页广告
        SpUtils.saveBoolean(
            AdsSwitch.adInterLaunchGender, FireBaseUtils.getBoolean(AdsSwitch.adInterLaunchGender)
        )
        //第一次启动性别选择页后_插页广告
        SpUtils.saveBoolean(
            AdsSwitch.adInterLaunchLanguage,
            FireBaseUtils.getBoolean(AdsSwitch.adInterLaunchLanguage)
        )
        //第一次启动引导Guide页后_插页广告
        SpUtils.saveBoolean(
            AdsSwitch.adInterLaunchIntro, FireBaseUtils.getBoolean(AdsSwitch.adInterLaunchIntro)
        )
        //第一次冷启动后_性别选择页
        SpUtils.saveBoolean(
            AdsSwitch.pageLaunchGender, FireBaseUtils.getBoolean(AdsSwitch.pageLaunchGender)
        )
        //开屏广告位
        SpUtils.saveBoolean(
            AdsSwitch.adOpenLaunch, FireBaseUtils.getBoolean(AdsSwitch.adOpenLaunch)
        )
        //激励广告位
        SpUtils.saveString(
            AdsSwitch.bp_rv_cd_s, FireBaseUtils.getString(AdsSwitch.bp_rv_cd_s)
        )
        //血压结果页原生广告位
        SpUtils.saveBoolean(
            AdsSwitch.adNativeBPResult, FireBaseUtils.getBoolean(AdsSwitch.adNativeBPResult)
        )
        //心率报告页原生广告位
        SpUtils.saveBoolean(
            AdsSwitch.adNativeHRReport, FireBaseUtils.getBoolean(AdsSwitch.adNativeHRReport)
        )
        //是否缓存插页广告
        SpUtils.saveBoolean(
            AdsSwitch.adInterCacheControl, FireBaseUtils.getBoolean(AdsSwitch.adInterCacheControl)
        )
    }
}